﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace homework7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (graphics == null)
                graphics = this.CreateGraphics();
            int n = 0;
            if(textBox1.Text!=null)
                n = Convert.ToInt32(textBox1.Text);
            int leng = 0;
            if (textBox2.Text != null)
                leng = Convert.ToInt32(textBox2.Text);

            drawCayleyTree(n, 200, 310, leng, -Math.PI / 2);
        }

        private Graphics graphics;
        double th1;
        double th2;
        double per1;
        double per2;

        void drawCayleyTree(int n,double x0,double y0,double leng,double th)
        {
            if (n == 0)
                return;
            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);

            drawLine(x0, y0, x1, y1);

            drawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1);
            drawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2);
        }

        void drawLine(double x0,double y0,double x1,double y1)
        {
            Pen pen = new Pen(colorDialog1.Color);
            graphics.DrawLine(pen, (int)x0, (int)y0, (int)x1, (int)y1);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            per1 = 0.0;
            if (textBox3.Text != null)
                per1 = Convert.ToDouble(textBox3.Text);
            per2 = 0.0;
            if (textBox4.Text != null)
                per2 = Convert.ToDouble(textBox4.Text);
            th1 = 0.0;
            if (textBox5.Text != null)
                th1 = Convert.ToDouble(textBox5.Text) * Math.PI / 180;
            th2 = 0.0;
            if (textBox5.Text != null)
                th2 = Convert.ToDouble(textBox6.Text) * Math.PI / 180;
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            if (textBox3.Text != null)
                per1 = Convert.ToDouble(textBox3.Text);
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            if (textBox4.Text != null)
                per2 = Convert.ToDouble(textBox4.Text);
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            if (textBox5.Text != null)
                th1 = Convert.ToDouble(textBox5.Text) * Math.PI / 180;
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {
            if (textBox5.Text != null)
                th2 = Convert.ToDouble(textBox6.Text) * Math.PI / 180;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
            Color c = colorDialog1.Color;
        }
    }
}
